﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Xml.Linq;
using System.Xml;

namespace WPFRadio
{

    
	/// <summary>
	/// Logique d'interaction pour PlaylistManager.xaml
	/// </summary>
    public partial class PlaylistManager : Window
	{

       
        Radio Radio = new Radio();
        public static  string DevID = "de1E_X56qLfGGj1U";
        public static  string UrlGenreBase = "http://api.shoutcast.com/legacy/genrelist?k=";
        public static  string UrlStationByGenre = "http://api.shoutcast.com/legacy/genresearch?k="; // [Your Dev ID]&genre=classic




       
     

		public PlaylistManager()
		{
			this.InitializeComponent();
            var wc = new WebClient();
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.Encoding = System.Text.Encoding.UTF8;
            wc.OpenReadAsync(new Uri("http://api.shoutcast.com/legacy/genrelist?k=de1E_X56qLfGGj1U"));
			
			// Insérez le code requis pour la création d’objet sous ce point.
		}

        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null || e.Cancelled)
            {
                ((TreeViewItem)_RadiosItem.Items[0]).Header = "Error Getting Web Radios...";
                return;

            }

            AddHandler(TreeViewItem.ExpandedEvent,
                      new RoutedEventHandler(TreeItemExpanded), true);

            var wc = (WebClient)sender;


         /*   if (wc.ResponseHeaders.Get(6) != "text/xml") /* TO FIX 6 or 5 or....
            {
                MessageBox.Show(wc.ResponseHeaders.Get(6)+" is not supported \nOnly text/xml is supported");
                return;
            }*/

            
            _RadiosItem.Items.Clear();
           // _RadiosItem.ItemsSource = Category.DeserializeXml(e.Result);



            _RadiosItem.ItemsSource = XmlParser.ParseXMLGenre(e.Result);

                 
           

            

        }

     










        private void TreeItemExpanded(object sender, RoutedEventArgs e)
        {

            var item = e.OriginalSource as TreeViewItem;
            if (item == null)
            { return; }
            var cat = item.DataContext as Category;
            if (cat == null)
            { return; }
            cat.LoadChildren();

        }
    }



}